package com.portal.business.news;

import java.util.ArrayList;
import java.util.List;

import com.portal.business.news.dao.NewsDao;
import com.portal.business.news.dto.NewsDTO;
import com.portal.business.news.dto.NewsResultBean;
import com.portal.framework.common.PortalSearchBean;
import com.portal.framework.page.Pager;


	
/**
 * 
 * 项目名称: Portal     
 * 类名称:   NewsServiceImpl     
 * 类描述:   TODO 
 * 创建人:   骆海东
 * 创建时间: Aug 31, 2011 10:10:42 AM     
 * 最后修改人:   骆海东
 * 最后修改时间: Aug 31, 2011 10:10:42 AM     
 * 最后修改备注:
 */
public class NewsServiceImpl implements NewsService {

	private NewsDao newsDao;
	
	public NewsDTO addNews(NewsDTO newsDTO) {
		return this.newsDao.create(newsDTO);
	}

	public int delNews(Integer newsId) {
		return this.newsDao.delete(newsId);
	}

	public int delNewsBatch(String[] asIdsStr) {
		int len=asIdsStr.length;
		Integer[] ids=new Integer[len];
		for (int i = 0; i <len; i++) {
			ids[i]=Integer.valueOf(asIdsStr[i]);
		}
		return this.newsDao.deleteBatch(ids);
	}

	public NewsDTO findByID(Integer newsId) {
		return this.newsDao.findById(newsId);
	}

	public NewsResultBean findNews(PortalSearchBean portalSearchBean) {

		String hql="from NewsDTO ";
		String countHql="select count(newsId) ";
		//用于保存查找输入参数集合
		List<Object> osList=new ArrayList<Object>();
		if(portalSearchBean.getName()!=null&&!portalSearchBean.getName().trim().equals("")){
			hql=hql+"where title like ?";
			osList.add("%"+portalSearchBean.getName()+"%");
		}
		hql=hql+" order by newsId desc";
		//获取总数
		int count=this.newsDao.countQuery(countHql+hql,osList.toArray());
		//设置页码对象
		Pager pager=new Pager();
		pager.setPageSize(portalSearchBean.getPageSize());
		pager.setTotal(count);
		pager.setCurrentPage(portalSearchBean.getPage());
		//获取数据
		List<NewsDTO> olsit=this.newsDao.find(pager.getStart(),pager.getPageSize(), NewsDTO.class, hql, osList.toArray());
		//设置返回对象
		NewsResultBean pb=new NewsResultBean();
		pb.setCount(count);
		pb.setResultList(olsit);
		return pb;
	}

	public List<NewsDTO> findIndex() {
		String hql="from NewsDTO order by newsId desc";
		return this.newsDao.find(0, 8, hql);
	}
	
	public NewsDTO updateNews(NewsDTO newsDTO) {
		return this.newsDao.update(newsDTO);
	}

	public NewsDao getNewsDao() {
		return newsDao;
	}

	public void setNewsDao(NewsDao newsDao) {
		this.newsDao = newsDao;
	}


}

		